//
//  BaseWebViewViewController.m
//  customer
//
//  Created by Holothurian on 2018/10/21.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "BaseWebViewViewController.h"
#import "WebViewProgressLine.h"

@interface BaseWebViewViewController ()<UIWebViewDelegate>

@property (nonatomic,strong)UIWebView *webView;

@property (nonatomic,strong)WebViewProgressLine *progressLine;

@end

@implementation BaseWebViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createMainView];
}

- (void)backTapClick{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else if(self.isPush == YES){
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)createMainView{
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, F_DEVICE_W, F_DEVICE_H-NAVIGATION_BAR_HEIGHT)];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    self.urlStr = [self.urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if (![self.urlStr containsString:@"https:"]) {
        self.urlStr = [NSString stringWithFormat:@"https://%@",self.urlStr];
    }
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr?:@""]]];
    self.progressLine = [[WebViewProgressLine alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, F_DEVICE_W, 3)];
    self.progressLine.colorLine = [UIColor blueColor];
    [self.view addSubview:self.progressLine];
    if (k_Is_Empty(_titleStr)) {
        NSString *webTitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        self.title = webTitle;
    }else{
        self.title = self.titleStr;
    }
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [self.progressLine startAnimation];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self.progressLine stopAnimation];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [self.progressLine stopAnimation];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
